Checkboxes
UI Behavior (Real Project Context)β
Checkboxes allow users to select one or more options independently. Unlike radio buttons, each checkbox maintains its own state.
Common real-world usages:
- Accept terms & conditions
- Select multiple preferences
- Feature enable/disable flags
- Filters (e-commerce, dashboards)
Typical HTML Patterns Youβll Seeβ
Standard Checkboxβ
<input type="checkbox" id="newsletter" value="yes">
<label for="newsletter">Subscribe to newsletter</label>
Grouped Checkboxesβ
<input type="checkbox" name="skills" value="java"> Java
<input type="checkbox" name="skills" value="selenium"> Selenium
<input type="checkbox" name="skills" value="api"> API
Custom-Styled Checkbox (Very Common)β
<input type="checkbox" id="darkMode" hidden>
<div class="checkbox-ui">Dark Mode</div>
Locating Checkboxes (Stable Approaches)β
Preferredβ
idlabel-based XPathname + value- CSS for UI wrappers (custom checkboxes)
WebElement newsletter = driver.findElement(By.id("newsletter"));
WebElement seleniumSkill = driver.findElement(
By.xpath("//input[@name='skills' and @value='selenium']")
);
Selecting & Unselecting Checkboxesβ
Safe Selection (Mandatory Pattern)β
if (!newsletter.isSelected()) {
newsletter.click();
}
Safe Unselectβ
if (newsletter.isSelected()) {
newsletter.click();
}
Toggle Scenarioβ
newsletter.click();
Verifying Checkbox Stateβ
Assert.assertTrue(newsletter.isSelected());
Validate Multiple Selectionsβ
List<WebElement> skills = driver.findElements(By.name("skills"));
int selected = 0;
for (WebElement skill : skills) {
if (skill.isSelected()) {
selected++;
}
}
Assert.assertTrue(selected >= 1);
Handling Custom Checkboxes (Hidden Inputs)β
Strategyβ
- Click visible UI element
- Validate hidden input state
WebElement darkModeUI = driver.findElement(By.cssSelector(".checkbox-ui"));
darkModeUI.click();
WebElement hiddenCheckbox = driver.findElement(By.id("darkMode"));
Assert.assertTrue(hiddenCheckbox.isSelected());
Common Mistakes ββ
- Clicking without checking
isSelected() - Assuming checkbox behavior is exclusive (radio-style)
- Using index-based XPath
- Not validating unselect behavior
- Ignoring hidden checkbox implementations
Best Practices β β
- Always check state before clicking
- Validate both select and unselect flows
- Prefer label-based locators
- Handle custom UI checkboxes explicitly
- Log checkbox state changes in test reports
Interview Notes π―β
Q: Difference between checkbox and radio button?
A: Checkbox allows multiple independent selections; radio buttons allow only one selection per group.
Q: How do you uncheck a checkbox?
A: Verify isSelected() and click only if itβs already selected.
Q: How do you handle custom checkboxes?
A: Click the visible UI wrapper and validate the hidden inputβs state.
Real-Project Tip π‘β
Always test checkbox behavior with form submission and backend validation β especially for compliance-related checkboxes.
Summaryβ
- Checkboxes support multiple selections
isSelected()is critical- Custom checkboxes are common in modern UIs
- Always test select and unselect scenarios